package com.gg.bananvideo.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.appcompat.app.AppCompatActivity;
import com.gg.bananvideo.FirstFragment;
import com.gg.bananvideo.R;
import com.gg.bananvideo.databinding.ActivityVideoBinding;
import com.gg.bananvideo.model.ListResponseData;
import com.gg.bananvideo.model.PlayMsg;
import com.gg.bananvideo.util.JsonUtil;
import com.google.android.exoplayer2.*;
import com.google.android.exoplayer2.ui.PlayerView;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

public class VideoActivity extends AppCompatActivity {

    private ActivityVideoBinding binding;
    private ExoPlayer player;
    private PlayerView playerView;


    private Handler handler = new Handler(msg -> {

        PlayMsg data = (PlayMsg) msg.obj;
        // 设置视频源
        Uri videoUri = Uri.parse(data.getData().getHttpurl());
        MediaItem mediaItem = MediaItem.fromUri(videoUri);

        player.setMediaItem(mediaItem);
        player.prepare();
        player.play();
        return false;
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        binding = ActivityVideoBinding.inflate(getLayoutInflater());
        Intent intent = getIntent();
        ListResponseData.Data.Vodrows data = intent.getParcelableExtra("data");
        playerView = binding.playerView;

        // 使用软件解码
        RenderersFactory renderersFactory = new DefaultRenderersFactory(this)
                .setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER);
        // 自定义LoadControl
        DefaultLoadControl loadControl = new DefaultLoadControl.Builder()
                //.setBufferForPlaybackAfterRebufferMs(2000)
                .setTargetBufferBytes(5000000) // 设置目标缓冲字节
                .setPrioritizeTimeOverSizeThresholds(true)
                .build();


        player = new ExoPlayer.Builder(this, renderersFactory).build();
        //player.setlo
        playerView.setPlayer(player);


        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("https://ios.bxguwen.com" + FirstFragment.datas.getPlay_url())
                .build();
        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String responseData = response.body().string();
                // 处理响应数据
                // 在这里更新UI或执行其他操作
                Message message = new Message();
                message.what = 1;
                message.obj = JsonUtil.toBean(responseData, PlayMsg.class);

                handler.sendMessage(message);
            }

            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                e.printStackTrace();
            }
        });
    }


    @Override
    protected void onStop() {
        super.onStop();
        player.release();
    }
}
